//
//  ViewController.m
//  testAudio
//
//  Created by zhenwei guan on 12-9-27.
//  Copyright (c) 2012年 hw. All rights reserved.
//

#import "ViewController.h"


@interface ViewController(Private)
// to be continued
@end

@implementation ViewController

@synthesize stopBtn;
@synthesize startBtn;
@synthesize ringtonePlayer;
@synthesize picker;
@synthesize pickerData;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) 
    {
        ringtonePlayer = [[RingtonePlayer alloc]initWithIndex:0 andLoopMode:NO];
        
//        //使应用可以后台运行
//        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
//        [audioSession setActive:YES error:nil];
        
        //使应用可以后台运行
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
        [audioSession setActive:YES error:nil];
    }
    return self;
}

//由nib初始化，因此INIT函数不会被调用
//- (id)init
//{
//    self = [super init];
//    if (self) 
//    {
//        NSString * path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"beyond.mp3"];
//        ringtonePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
//    }
//    return self;
//}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // BOOL isAble = [nil isEqualToString:nil];
    
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}


#pragma mark - picker delegates
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 3;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString * title;
    switch (row)
    {
        case 0:
            title = @"beyond";
            break;
        case 1:
            title = @"心跳";
            break;
        case 2:
            title = @"绝不放手";
            break;
        default:
            title = @"beyond";
            break;
    }
    return title;
}

/*- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
} */

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (row)
    {
        case 0:
            [ringtonePlayer changeRingtoneToName:@"beyond.mp3"];
            [ringtonePlayer startPlay];
            break;
        case 1:
            [ringtonePlayer changeRingtoneToName:@"xintiao.mp3"];
            [ringtonePlayer startPlay];
            break;
        case 2:
            [ringtonePlayer changeRingtoneToName:@"juebufangshou.mp3"];
            [ringtonePlayer startPlay];
            break;
        default:
            break;
    }
}


#pragma mark - custom methods

- (IBAction)startPlay:(id)sender
{
    [ringtonePlayer startPlay];
}

- (IBAction)stopPlay:(id)sender
{
   [ringtonePlayer stopPlay];
}



@end
